package cn.lizemin.demo.clone;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class Dog implements Cloneable{
    private String name ; //
    private Integer age ; //
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

    public void shout(){
        System.out.println("旺旺旺...");
    }
    /*@Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
*/
    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    //这是一个main方法,是程序的入口
    public static void main(String[] args) throws CloneNotSupportedException {
        test1();
    }

    private static void test1() throws CloneNotSupportedException {
        Dog puppy = new Dog("puppy", 2);
        //向下转型
        Dog puppy2 = (Dog) puppy.clone();
        //两个对象的地址不同，克隆成功
        System.out.println(puppy2);
        System.out.println(puppy);
        //通过克隆对象调用方法和属性
        puppy2.shout();
        System.out.println(puppy2.getName());

    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
